<?php

require_once("./lib/pluginbase.php");

class BanPlugin extends PluginBase
{
	//needs table banned values(username varchar(100))
	protected $db;
	
	function checkConnection()
	{
		if(!$this->db)
		{
			$sqliteerror = "";
			if ($this->db = new SQLite3('banned.db', 0666, $sqliteerror))
			{
				$query = $this->db->querySingle("SELECT name FROM sqlite_master WHERE type='table' and name='banned';");
				if ($query == NULL)
				{
					echo "say Creating banned table\n";
					$this->db->querySingle("CREATE TABLE banned (username string, reason string, banned integer);");
				}
				return true;
			}
			else
			{
				fwrite(STDERR, "Failed to open SQLite Database: " . $sqliteerror . "\n");
				return false;
			}
		}
		return true;
	}

        function isBanned($userorip)
	{
		if (!$this->checkConnection())
			return false;
                $results = $this->db->querySingle("SELECT reason FROM banned where username ='$userorip' AND banned = '1' limit 1;");
		return ($results != NULL ? stripslashes($results["reason"]) : false);
	}

        function banThem($who, $reason)
	{
		$who = sqlite_escape_string($who);
		$reason = sqlite_escape_string($reason);
		fwrite(STDERR, "Banning $who '$reason'\n");
		if (!$this->checkConnection())
			return false;
		$this->db->exec("DELETE FROM banned WHERE username = '$who';");
                $this->db->exec("INSERT INTO banned (username, reason, banned) values ('$who', '$reason', '1');");
		fwrite(STDERR, "Banned $who\n");
        }

	function unBan($who)
	{
		if (!$this->checkConnection())
			return false;
                $this->db->exec("update banned SET banned = '0' where username = '$who';");
	}

        function processUserJoin($user,$ip)
	{
		if (!$this->checkConnection())
			return false;
		$ipnoport = split(':',$ip);  // gets rid of the port.. until chris changes the regex to not return the port
		if (($reason = $this->isBanned($user)) !== false)
		{
			fwrite(STDERR, "Autokicking banned user $user: " . $reason . "\n");
			echo "kick $user\n";
		}
		if (($reason = $this->isBanned($ipnoport[0])) !== false)
		{
			fwrite(STDERR, "Autokicking banned IP " . $ipnoport[0] . ": " . $reason . "\n");
			echo "kick $user\n";
		}
        }
        function processChatCommand($name, $cmd, $args)
	{
		$admin = $this->isAdmin($name);
		switch($cmd)
		{
			case "!ban":
				if (!$admin)
					break;
				if(count($args) < 2)
				{
					echo "say Usage: !ban user|ip reason\n";
					break;
				}
				else
				{
					if ($this->isBanned($args[0]))
					{
						echo "say $args[0] is already banned.\n";
					}
					else
					{
						$reason = $args;
						unset($reason[0]);
						$reason = implode(" ", $reason);
						$this->banThem($args[0], $reason);
						echo "say $args[0] has been banned.\n";
						echo "kick $args[0]\n";
						break;
					}
				}
				break;
			case "!unban":
				if (!$admin)
					break;
				if(count($args) < 1)
				{
					echo "say Usage: !unban user|ip\n";
					break;
				}
				else
				{
				      if ($this->isBanned($args[0])){
						echo "say unbanning $args[0]\n";
						$this->unBan($args[0]);
					}
					else
					{
						echo "say That user is not banned\n";
					}
				}
		}
        }
}

registerPlugin("ban", new BanPlugin);



